package com.zyh.service.impl;

import com.zyh.dao.FundPayMapper;
import com.zyh.dao.TrandMapper;
import com.zyh.pojo.*;
import com.zyh.service.FundPayGroupService;
import com.zyh.service.FundPayService;
import com.zyh.util.RandomNumber;
import com.zyh.util.XstreamKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.*;

/**
 * @author wangchengshou
 * @version 1.0
 * @date 2020/12/7 11:02
 */
@Service
public class FundPayGroupServiceImpl implements FundPayGroupService {

    @Autowired
    private FundPayMapper fundPayMapper;

    @Autowired
    private TrandMapper trandMapper;

    @Autowired
    private FundPayService fundPayService;

    //基金支付指令
    @Override
    public String fundPayOrder(String reqTime, String serialNo, String ybXml, OutMessagePojo outMessagePojo, final TransferAccountsPojo pojo) {
        //行号
        String bankNo = pojo.getUnionBankNo() == null ? "" : pojo.getUnionBankNo();
        //转入卡号
        String cardNo = pojo.getPayeeAcNo();
        //如果有12位行号走跨行转账  如果卡号前六位是621065 623213 621517 623017走对公户转账（卡）
        //如果卡号前二位是80 81 82走对公户转账（对公）
        String twoCardNo = cardNo.substring(0, 2);
        String sixCardNo = cardNo.substring(0, 6);

        if (bankNo.length() == 12) {

            return interBankTransfer(reqTime, serialNo, ybXml, outMessagePojo, pojo);
        } else if ("621065".equals(sixCardNo) || "623213".equals(sixCardNo) || "621517".equals(sixCardNo) || "623017".equals(sixCardNo)) {

            return revolutionToCard(reqTime, serialNo, ybXml, outMessagePojo, pojo);
        } else if ("80".equals(twoCardNo) || "81".equals(twoCardNo) || "82".equals(twoCardNo)) {

            return publicTransfer(reqTime, serialNo, ybXml, outMessagePojo, pojo);
        }

        //返回错误报文
        OutHeadPojo outHeadPojo = outMessagePojo.getHead();
        outHeadPojo.setIBSReturnMsg("失败");
        outHeadPojo.setIBSJnlNo(RandomNumber.productBsnSrNo().substring(14, 32));

        OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
        outBodyTransferPojo.setOrderState("91");
        outBodyTransferPojo.setOrderFlowDescript("账号不符合");
        //日志落库
        logFellLibrary("", "", "", "91", "账号不符合", RandomNumber.productBsnSrNo(), "", "", "", "", XstreamKit.vo2Xml(outMessagePojo), ybXml);

        //将基金支付状态存库
        addTransactionStatus("", reqTime, serialNo, RandomNumber.productBsnSrNo(), "91", "转账失败", "0");

        return XstreamKit.vo2Xml(outMessagePojo);
    }


    /**
     * 跨行转账
     */
    private String interBankTransfer(String reqTime, String serialNo, String ybXml, OutMessagePojo outMessagePojo, final TransferAccountsPojo pojo) {
        //支付状态落库的全局流水号（流水号）
        String retBsnSrlNo = "";
        //支付状态落库的message
        String retMessage = "";
        // 判断金额大于100万走大额  传网点号(余额查询宇航)/付款账号/付款金额/  综合前置系统
        // 小于等于走超网
        //查询付款账户名称
        Map<String, Object> checkAccountMap = fundPayService.checkAccountBalance(pojo.getPayerAcNo());
        retBsnSrlNo = (String) checkAccountMap.get("bsnSrlNo");
        retMessage = (String) checkAccountMap.get("message");
        if (checkAccountMap.containsKey("success")) {
            System.out.println("查询付款账户名称成功，流水号：  " + retBsnSrlNo);
            //日志落库
            logFellLibrary("QRCB004", checkAccountMap.get("txnDt"), checkAccountMap.get("txnTm"), "90", "查询付款账号名称成功:" + retMessage, retBsnSrlNo, checkAccountMap.get("retCode"), "30013000101", checkAccountMap.get("esbOut"), checkAccountMap.get("esbIn"), "", ybXml);
            if (pojo.getAmount() > 1000000.0) {
                //大额支付
                Map<String, Object> tramsferBigMap = fundPayService.interbankTramsferBig(RandomNumber.productBsnSrNo(), pojo.getPayerAcNo(), (String) checkAccountMap.get("cstNm"), pojo.getPayeeAcNo(), pojo.getPayeeAcName(), pojo.getUnionBankNo(), pojo.getUnionBankName(), String.valueOf(pojo.getAmount()), pojo.getUnionBankNo());
                retBsnSrlNo = (String) tramsferBigMap.get("bsnSrlNo");
                retMessage = (String) tramsferBigMap.get("message");
                if (tramsferBigMap.containsKey("success")) {
                    System.out.println("大额成功,流水号：  " + retBsnSrlNo);
                    //日志落库
                    logFellLibrary("QRCB004", tramsferBigMap.get("txnDt"), tramsferBigMap.get("txnTm"), "90", "大额支付成功:" + retMessage, retBsnSrlNo, tramsferBigMap.get("retCode"), "30041000803", tramsferBigMap.get("esbOut"), tramsferBigMap.get("esbIn"), "", ybXml);

                    //还要执行大额查询--查询支付状态
                    Map<String, Object> bigLogMap = fundPayService.interbankTramsferBigLog((String) tramsferBigMap.get("cnlSrlNo"));
                    retBsnSrlNo = (String) bigLogMap.get("bsnSrlNo");
                    retMessage = (String) bigLogMap.get("message");
                    if (bigLogMap.containsKey("success")) {
                        System.out.println("大额查询成功,流水号：  " + retBsnSrlNo);
                        OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                        outHeadPojo.setIBSReturnMsg("成功");
                        outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                        OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                        outBodyTransferPojo.setOrderState((String)bigLogMap.get("srlSt"));
                        outBodyTransferPojo.setOrderFlowDescript((String)bigLogMap.get("srlStName"));
                        //日志落库
                        logFellLibrary("QRCB004", bigLogMap.get("txnDt"), bigLogMap.get("txnTm"), bigLogMap.get("srlSt"), (String)bigLogMap.get("srlStName") + retMessage, retBsnSrlNo, bigLogMap.get("retCode"), "30063000101", bigLogMap.get("esbOut"), bigLogMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                        //将基金支付状态存库
                        addTransactionStatus("QRCB004", reqTime, serialNo, retBsnSrlNo, (String)bigLogMap.get("srlSt"), (String)bigLogMap.get("srlStName") + retMessage, "1");
                        return XstreamKit.vo2Xml(outMessagePojo);
                    } else {
                        System.out.println("大额查询--查询支付状态失败,流水号：  " + retBsnSrlNo);
                        OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                        outHeadPojo.setIBSReturnMsg("失败");
                        outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                        OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                        outBodyTransferPojo.setOrderState("91");
                        outBodyTransferPojo.setOrderFlowDescript("转账失败");
                        //日志落库
                        logFellLibrary("QRCB004", bigLogMap.get("txnDt"), bigLogMap.get("txnTm"), "91", "大额查询--查询支付状态失败:" + retMessage, retBsnSrlNo, bigLogMap.get("retCode"), "30063000101", bigLogMap.get("esbOut"), bigLogMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                        //将基金支付状态存库
                        addTransactionStatus("QRCB004", reqTime, serialNo, retBsnSrlNo, "91", "大额查询--查询支付状态失败:" + retMessage, "1");
                        return XstreamKit.vo2Xml(outMessagePojo);
                    }

                } else {
                    System.out.println("大额支付失败,流水号：  " + retBsnSrlNo);
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("失败");
                    outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("91");
                    outBodyTransferPojo.setOrderFlowDescript("转账失败");
                    //日志落库
                    logFellLibrary("QRCB004", tramsferBigMap.get("txnDt"), tramsferBigMap.get("txnTm"), "91", "大额支付失败:" + retMessage, retBsnSrlNo, tramsferBigMap.get("retCode"), "30041000803", tramsferBigMap.get("esbOut"), tramsferBigMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB004", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "1");
                    return XstreamKit.vo2Xml(outMessagePojo);
                }
            } else {
                //超级网银转账
                Map<String, Object> tramsferMap = fundPayService.interbankTramsfer(pojo.getPayerAcNo(), (String) checkAccountMap.get("cstNm"), (String) checkAccountMap.get("acctOpnOtltsNo"), pojo.getUnionBankNo(), pojo.getPayeeAcNo(), pojo.getPayeeAcName(), String.valueOf(pojo.getAmount()));
                retBsnSrlNo = (String) tramsferMap.get("bsnSrlNo");
                retMessage = (String) tramsferMap.get("message");
                if (tramsferMap.containsKey("success")) {
                    System.out.println("超网转账成功！");
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("成功");
                    outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("90");
                    outBodyTransferPojo.setOrderFlowDescript("转账成功");

                    //日志落库
                    logFellLibrary("QRCB003", tramsferMap.get("txnDt"), tramsferMap.get("txnTm"), "90", "转账成功:" + retMessage, retBsnSrlNo, tramsferMap.get("retCode"), "30041000804", tramsferMap.get("esbOut"), tramsferMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "90", "超网转账成功:" + retMessage, "0");
                    return XstreamKit.vo2Xml(outMessagePojo);
                } else {
                    System.out.println("小于100万失败！");
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("失败");
                    outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("91");
                    outBodyTransferPojo.setOrderFlowDescript("转账失败");
                    //日志落库
                    logFellLibrary("QRCB003", tramsferMap.get("txnDt"), tramsferMap.get("txnTm"), "91", "转账失败:" + retMessage, retBsnSrlNo, tramsferMap.get("retCode"), "30041000804", tramsferMap.get("esbOut"), tramsferMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);

                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                    return XstreamKit.vo2Xml(outMessagePojo);
                }
            }
        } else {
            System.out.println("付款账号名称查询失败！");
            OutHeadPojo outHeadPojo = outMessagePojo.getHead();
            outHeadPojo.setIBSReturnMsg("失败");
            outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

            OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
            outBodyTransferPojo.setOrderState("91");
            outBodyTransferPojo.setOrderFlowDescript("转账失败");
            //日志落库
            logFellLibrary("QRCB004", checkAccountMap.get("txnDt"), checkAccountMap.get("txnTm"), "91", "查询付款账号名称失败:" + retMessage, retBsnSrlNo, checkAccountMap.get("retCode"), "30013000101", checkAccountMap.get("esbOut"), checkAccountMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);

            //将基金支付状态存库
            addTransactionStatus("QRCB004", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "1");
            return XstreamKit.vo2Xml(outMessagePojo);
        }
    }


    /**
     * 对公转对卡
     */
    private String revolutionToCard(String reqTime, String serialNo, String ybXml, OutMessagePojo outMessagePojo, final TransferAccountsPojo pojo) {
        //支付状态落库的全局流水号（流水号）
        String retBsnSrlNo = "";
        //支付状态落库的message
        String retMessage = "";
        //对公转对卡
        //卡信息查询——获取网点号
        Map<String, Object> cardInfoMap = fundPayService.queryCardInfo(pojo.getPayeeAcNo());
        retBsnSrlNo = (String) cardInfoMap.get("bsnSrlNo");
        retMessage = (String) cardInfoMap.get("message");
        if (cardInfoMap.containsKey("success")) {
            System.out.println("卡信息查询成功,流水号:  " + retBsnSrlNo);
            BasicAccountPojo basicAccountPojo = (BasicAccountPojo) cardInfoMap.get("success");
            pojo.setOtltsCd(basicAccountPojo.getOtltsCd().substring(1));
            //日志落库
            logFellLibrary("QRCB003", cardInfoMap.get("txnDt"), cardInfoMap.get("txnTm"), "90", "卡信息查询——获取网点号成功:" + retMessage, retBsnSrlNo, cardInfoMap.get("retCode"), "30033000201", cardInfoMap.get("esbOut"), cardInfoMap.get("esbIn"), "", ybXml);

            if (!pojo.getPayeeAcName().trim().equals(basicAccountPojo.getCrdHldrNm().trim())) {
                OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                outHeadPojo.setIBSReturnMsg("失败");
                outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                outBodyTransferPojo.setOrderState("91");
                outBodyTransferPojo.setOrderFlowDescript("收款人姓名和请求报文中的姓名不一致");
                //日志落库
                logFellLibrary("QRCB003", cardInfoMap.get("txnDt"), cardInfoMap.get("txnTm"), "91", "收款人姓名和请求报文中的姓名不一致", retBsnSrlNo, cardInfoMap.get("retCode"), "30033000201", cardInfoMap.get("esbOut"), cardInfoMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                //将基金支付状态存库
                addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败", "0");
                //返回错误报文
                return XstreamKit.vo2Xml(outMessagePojo);
            }
        } else {
            System.out.println("卡信息查询失败,流水号:  " + retBsnSrlNo);
            OutHeadPojo outHeadPojo = outMessagePojo.getHead();
            outHeadPojo.setIBSReturnMsg("失败");
            outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

            OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
            outBodyTransferPojo.setOrderState("91");
            outBodyTransferPojo.setOrderFlowDescript("转账失败");
            //日志落库
            logFellLibrary("QRCB003", cardInfoMap.get("txnDt"), cardInfoMap.get("txnTm"), "91", "卡信息查询——获取网点号失败:" + retMessage, retBsnSrlNo, cardInfoMap.get("retCode"), "30033000201", cardInfoMap.get("esbOut"), cardInfoMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
            //将基金支付状态存库
            addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
            //返回错误报文
            return XstreamKit.vo2Xml(outMessagePojo);
        }
        //核心记账
        Callable<String> call = new Callable<String>() {
            @Override
            public String call() throws Exception {
                String tag = "0000";
                System.out.println("正在执行核心记账...");
                Map<String, Object> chargeMap = fundPayService.chargeToAccount(pojo.getPayerAcNo(), pojo.getAmount(), pojo.getOtltsCd());
                if (chargeMap.containsKey("success")) {

                } else {
                    tag = "1111";
                    tag += chargeMap.get("error");
                }
                pojo.setTxnSrlNo((String) chargeMap.get("txnSrlNo"));
                pojo.setTxnDt((String) chargeMap.get("txnDt"));
                pojo.setTxnTm((String) chargeMap.get("txnTm"));
                pojo.setBsnSrlNo((String) chargeMap.get("bsnSrlNo"));
                pojo.setMap(chargeMap);
                //Thread.sleep(5 * 1000);
                return tag;
            }
        };

        FutureTask<String> task = new FutureTask<>(call);
        Thread thread = new Thread(task);
        thread.start();
        String result = null;
        try {
            result = task.get(30, TimeUnit.SECONDS);
            Map<String, Object> chargeMap = pojo.getMap();
            retBsnSrlNo = pojo.getBsnSrlNo();
            retMessage = (String) chargeMap.get("message");
            if ("0000".equals(result)) {
                System.out.println("核心记账--无折客户转账内部帐成功,流水号:  " + pojo.getBsnSrlNo());
                //日志落库
                logFellLibrary("QRCB003", chargeMap.get("txnDt"), chargeMap.get("txnTm"), "90", "核心记账--无折客户转账内部帐成功:" + retMessage, retBsnSrlNo, chargeMap.get("retCode"), "30011000401", chargeMap.get("esbOut"), chargeMap.get("esbIn"), "", ybXml);

                System.out.println("开始银联数据记账......");
                //银联数据记账
                Map<String, Object> unionpayMap = fundPayService.unionpayDataByArgs(pojo.getOtltsCd(), pojo.getPayeeAcNo(),String.valueOf(pojo.getAmount()));
                retBsnSrlNo = (String) unionpayMap.get("bsnSrlNo");
                retMessage = (String) unionpayMap.get("message");
                if (unionpayMap.containsKey("success")) {
                    System.out.println("银联数据记账成功,流水号：  " + retBsnSrlNo);
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("成功");
                    outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("90");
                    outBodyTransferPojo.setOrderFlowDescript("转账成功");
                    //日志落库
                    logFellLibrary("QRCB003", unionpayMap.get("txnDt"), unionpayMap.get("txnTm"), "90", "银联数据记账成功:" + retMessage, retBsnSrlNo, unionpayMap.get("retCode"), "30011000101", unionpayMap.get("esbOut"), unionpayMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "90", "转账成功:" + retMessage, "0");
                    //返回错误报文
                    return XstreamKit.vo2Xml(outMessagePojo);
                } else {
                    System.out.println("银联数据记账失败,流水号：  " + retBsnSrlNo);
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("失败");
                    outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("91");
                    outBodyTransferPojo.setOrderFlowDescript("转账失败");

                    //日志落库
                    logFellLibrary("QRCB003", unionpayMap.get("txnDt"), unionpayMap.get("txnTm"), "91", "银联数据记账失败:" + retMessage, retBsnSrlNo, unionpayMap.get("retCode"), "30011000101", unionpayMap.get("esbOut"), unionpayMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                    //返回错误报文
                    return XstreamKit.vo2Xml(outMessagePojo);
                }
            } else {
                System.out.println("核心记账--无折客户转账内部帐失败,流水号：  " + pojo.getBsnSrlNo());
                OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                outHeadPojo.setIBSReturnMsg("失败");
                outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                outBodyTransferPojo.setOrderState("91");
                outBodyTransferPojo.setOrderFlowDescript("转账失败");

                //日志落库
                logFellLibrary("QRCB003", chargeMap.get("txnDt"), chargeMap.get("txnTm"), "91", "核心记账--无折客户转账内部帐失败:" + retMessage, retBsnSrlNo, chargeMap.get("retCode"), "30011000401", chargeMap.get("esbOut"), chargeMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                //将基金支付状态存库
                addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                //返回错误报文
                return XstreamKit.vo2Xml(outMessagePojo);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
            System.out.println("超时了");
            //核心记账超时后调用核心记账冲正
            System.out.println("超时数据" + pojo.getTxnSrlNo());
            System.out.println("正在执行核心记账超时后调用核心记账冲正...");
            Map<String, Object> accountMap = fundPayService.coreAccounting(pojo.getBsnSrlNo(), pojo.getPayerAcNo(), pojo.getAmount(), pojo.getOtltsCd(), pojo.getTxnSrlNo(), pojo.getTxnDt(), pojo.getTxnTm());
            retBsnSrlNo = (String) accountMap.get("bsnSrlNo");
            retMessage = (String) accountMap.get("message");
            if (accountMap.containsKey("success")) {
                OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                outHeadPojo.setIBSReturnMsg("失败");
                outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                outBodyTransferPojo.setOrderState("91");
                outBodyTransferPojo.setOrderFlowDescript("转账失败");
                System.out.println("核心记账超时后调用核心记账超时冲正成功,流水号:  " + retBsnSrlNo);
                //日志落库
                logFellLibrary("QRCB003", accountMap.get("txnDt"), accountMap.get("txnTm"), "90", "核心记账--无折客户转账内部帐超时冲正成功:" + retMessage, retBsnSrlNo, accountMap.get("retCode"), "30014000102", accountMap.get("esbOut"), accountMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                //将基金支付状态存库
                addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                //返回错误报文
                return XstreamKit.vo2Xml(outMessagePojo);
            } else {
                OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                outHeadPojo.setIBSReturnMsg("失败");
                outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                outBodyTransferPojo.setOrderState("91");
                outBodyTransferPojo.setOrderFlowDescript("转账失败");
                System.out.println("核心记账超时后调用核心记账超时冲正失败,流水号:  " + retBsnSrlNo);
                //日志落库
                logFellLibrary("QRCB003", accountMap.get("txnDt"), accountMap.get("txnTm"), "91", "核心记账--无折客户转账内部帐超时冲正失败:" + retMessage, retBsnSrlNo, accountMap.get("retCode"), "30014000102", accountMap.get("esbOut"), accountMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                //将基金支付状态存库
                addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                //返回错误报文
                return XstreamKit.vo2Xml(outMessagePojo);
            }
        }
        //返回错误报文
        OutHeadPojo outHeadPojo = outMessagePojo.getHead();
        outHeadPojo.setIBSReturnMsg("失败");
        outHeadPojo.setIBSJnlNo(RandomNumber.productBsnSrNo().substring(14, 32));

        OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
        outBodyTransferPojo.setOrderState("91");
        outBodyTransferPojo.setOrderFlowDescript("账号不符合");
        //日志落库
        logFellLibrary("", "", "", "91", "账号不符合", RandomNumber.productBsnSrNo(), "", "", "", "", XstreamKit.vo2Xml(outMessagePojo), ybXml);

        //将基金支付状态存库
        addTransactionStatus("QRCB003", reqTime, serialNo, RandomNumber.productBsnSrNo(), "91", "转账失败", "0");
        return XstreamKit.vo2Xml(outMessagePojo);
    }

    /**
     * 对公转账
     */
    private String publicTransfer(String reqTime, String serialNo, String ybXml, OutMessagePojo outMessagePojo, final TransferAccountsPojo pojo) {
        //支付状态落库的全局流水号（流水号）
        String retBsnSrlNo = "";
        //支付状态落库的message
        String retMessage = "";
        if (pojo.getPayeeAcNo().length() != 17) {
            OutHeadPojo outHeadPojo = outMessagePojo.getHead();
            outHeadPojo.setIBSReturnMsg("失败");
            outHeadPojo.setIBSJnlNo(RandomNumber.productBsnSrNo().substring(14, 32));

            OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
            outBodyTransferPojo.setOrderState("91");
            outBodyTransferPojo.setOrderFlowDescript("账号不合法请重新输入");
            //日志落库
            logFellLibrary("QRCB003", RandomNumber.productTxnDt(), RandomNumber.productTxnTm(), "91", "账号不合法请重新输入", "", "", "", "", "", XstreamKit.vo2Xml(outMessagePojo), ybXml);
            //将基金支付状态存库
            addTransactionStatus("QRCB003", reqTime, serialNo, RandomNumber.productBsnSrNo(), "91", "转账失败--账号不合法", "0");
            //返回错误报文
            return XstreamKit.vo2Xml(outMessagePojo);
        }
        Map<String, Object> checkAccountBalance = fundPayService.checkAccountBalance(pojo.getPayeeAcNo());
        retBsnSrlNo = (String) checkAccountBalance.get("bsnSrlNo");
        retMessage = (String) checkAccountBalance.get("message");
        if (checkAccountBalance.containsKey("success")) {
            if (!pojo.getPayeeAcName().trim().equals(checkAccountBalance.get("cstNm"))) {
                OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                outHeadPojo.setIBSReturnMsg("失败");
                outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                outBodyTransferPojo.setOrderState("91");
                outBodyTransferPojo.setOrderFlowDescript("收款人姓名和请求报文中的姓名不一致");
                //日志落库
                logFellLibrary("QRCB003", checkAccountBalance.get("txnDt"), checkAccountBalance.get("txnTm"), "91", "收款人姓名和请求报文中的姓名不一致", retBsnSrlNo, checkAccountBalance.get("retCode"), "30013000101", checkAccountBalance.get("esbOut"), checkAccountBalance.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                //将基金支付状态存库
                addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败", "0");
                //返回错误报文
                return XstreamKit.vo2Xml(outMessagePojo);
            }
            Callable<String> call = new Callable<String>() {
                @Override
                public String call() throws Exception {
                    String tag = "0000";
                    System.out.println("正在执行对公核心记账...");

                    Map<String, Object> moneyTransferMap = fundPayService.moneyTransfer(pojo.getPayerAcNo(), String.valueOf(pojo.getAmount()), pojo.getPayeeAcNo());
                    if (moneyTransferMap.containsKey("success")) {
                    } else {
                        tag = "1111";
                        tag += moneyTransferMap.get("error");
                    }
                    pojo.setBsnSrlNo((String) moneyTransferMap.get("bsnSrlNo"));
                    pojo.setTxnDt((String) moneyTransferMap.get("txnDt"));
                    pojo.setTxnTm((String) moneyTransferMap.get("txnTm"));
                    pojo.setMap(moneyTransferMap);
                    //Thread.sleep(6*1000);
                    return tag;
                }
            };

            FutureTask<String> task = new FutureTask<>(call);
            Thread thread = new Thread(task);
            thread.start();
            String result = null;
            try {
                result = task.get(30, TimeUnit.SECONDS);
                Map<String, Object> moneyTransferMap = pojo.getMap();
                retBsnSrlNo = pojo.getBsnSrlNo();
                retMessage = (String) moneyTransferMap.get("message");
                if ("0000".equals(result)) {
                    System.out.println("核心记账--资金转账成功,流水号:  " + pojo.getBsnSrlNo());
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("成功");
                    outHeadPojo.setIBSJnlNo(pojo.getBsnSrlNo().substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("90");
                    outBodyTransferPojo.setOrderFlowDescript("转账成功");

                    //日志落库
                    logFellLibrary("QRCB003", moneyTransferMap.get("txnDt"), moneyTransferMap.get("txnTm"), "90", "核心记账--资金转账成功:" + retMessage, retBsnSrlNo, moneyTransferMap.get("retCode"), "30011000412", moneyTransferMap.get("esbOut"), moneyTransferMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "90", "转账成功:" + retMessage, "0");
                    //返回成功报文
                    return XstreamKit.vo2Xml(outMessagePojo);
                } else {
                    System.out.println("核心记账--资金转账失败,流水号:  " + pojo.getBsnSrlNo());
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("失败");
                    outHeadPojo.setIBSJnlNo(pojo.getBsnSrlNo().substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("91");
                    outBodyTransferPojo.setOrderFlowDescript("转账失败");

                    //日志落库
                    logFellLibrary("QRCB003", moneyTransferMap.get("txnDt"), moneyTransferMap.get("txnTm"), "91", "核心记账--资金转账失败:" + retMessage, retBsnSrlNo, moneyTransferMap.get("retCode"), "30011000412", moneyTransferMap.get("esbOut"), moneyTransferMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                    //返回错误报文
                    return XstreamKit.vo2Xml(outMessagePojo);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                System.out.println("超时了");
                System.out.println("正在执行对公核心记账冲正...");
                //对公户转账（对公）核心记账超时后调用对公户转账（对公）核心记账冲正
                Map<String, Object> fundMap = fundPayService.fundTransfer(pojo.getPayerAcNo(), String.valueOf(pojo.getAmount()), pojo.getPayeeAcNo(), pojo.getBsnSrlNo(), pojo.getTxnDt(), pojo.getTxnTm());
                retBsnSrlNo = (String) fundMap.get("bsnSrlNo");
                retMessage = (String) fundMap.get("message");
                if (fundMap.containsKey("success")) {
                    System.out.println("对公户转账（对公）核心记账超时后调用对公户转账（对公）核心记账冲正成功,流水号:  " + retBsnSrlNo);
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("失败");
                    outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("91");
                    outBodyTransferPojo.setOrderFlowDescript("转账失败");

                    //日志落库
                    logFellLibrary("QRCB003", fundMap.get("txnDt"), fundMap.get("txnTm"), "90", "对公户转账（对公）核心记账超时冲正成功:" + retMessage, retBsnSrlNo, fundMap.get("retCode"), "30014000113", fundMap.get("esbOut"), fundMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                    //返回错误报文
                    return XstreamKit.vo2Xml(outMessagePojo);
                } else {
                    OutHeadPojo outHeadPojo = outMessagePojo.getHead();
                    outHeadPojo.setIBSReturnMsg("失败");
                    outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

                    OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
                    outBodyTransferPojo.setOrderState("91");
                    outBodyTransferPojo.setOrderFlowDescript("转账失败");

                    System.out.println("对公户转账（对公）核心记账超时后调用对公户转账（对公）核心记账冲正失败,流水号:  " + retBsnSrlNo);
                    //日志落库
                    logFellLibrary("QRCB003", fundMap.get("txnDt"), fundMap.get("txnTm"), "91", "对公户转账（对公）核心记账超时冲正失败:" + retMessage, retBsnSrlNo, fundMap.get("retCode"), "30014000113", fundMap.get("esbOut"), fundMap.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
                    //将基金支付状态存库
                    addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
                    //返回错误报文
                    return XstreamKit.vo2Xml(outMessagePojo);
                }
            }
        } else {
            System.out.println("查询对公账户名称失败,流水号:  " + retBsnSrlNo);

            OutHeadPojo outHeadPojo = outMessagePojo.getHead();
            outHeadPojo.setIBSReturnMsg("失败");
            outHeadPojo.setIBSJnlNo(retBsnSrlNo.substring(14, 32));

            OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
            outBodyTransferPojo.setOrderState("91");
            outBodyTransferPojo.setOrderFlowDescript("查询对公账户名称失败--" + retMessage);

            //日志落库
            logFellLibrary("QRCB003", checkAccountBalance.get("txnDt"), checkAccountBalance.get("txnTm"), "91", "查询对公账户名称失败:" + retMessage, retBsnSrlNo, checkAccountBalance.get("retCode"), "30013000101", checkAccountBalance.get("esbOut"), checkAccountBalance.get("esbIn"), XstreamKit.vo2Xml(outMessagePojo), ybXml);
            //将基金支付状态存库
            addTransactionStatus("QRCB003", reqTime, serialNo, retBsnSrlNo, "91", "转账失败:" + retMessage, "0");
            //返回错误报文
            return XstreamKit.vo2Xml(outMessagePojo);
        }
        //返回错误报文
        OutHeadPojo outHeadPojo = outMessagePojo.getHead();
        outHeadPojo.setIBSReturnMsg("失败");
        outHeadPojo.setIBSJnlNo(RandomNumber.productBsnSrNo().substring(14, 32));

        OutBodyTransferPojo outBodyTransferPojo = outMessagePojo.getOutBodyTransferPojo();
        outBodyTransferPojo.setOrderState("91");
        outBodyTransferPojo.setOrderFlowDescript("账号不符合");
        //日志落库
        logFellLibrary("", "", "", "91", "账号不符合", RandomNumber.productBsnSrNo(), "", "", "", "", XstreamKit.vo2Xml(outMessagePojo), ybXml);

        //将基金支付状态存库
        addTransactionStatus("QRCB003", reqTime, serialNo, RandomNumber.productBsnSrNo(), "91", "转账失败", "0");

        return XstreamKit.vo2Xml(outMessagePojo);
    }

    /**
     * 基金支付状态存库
     */
    private void addTransactionStatus(String interFaceNo, String createTime, String serialNo, String bsnSrlNo, String status, String statusMessage, String isBig) {
        FundPayPoJo fundPayPoJo = new FundPayPoJo();
        fundPayPoJo.setINTERFACENO(interFaceNo);
        fundPayPoJo.setSerialNo(serialNo);
        fundPayPoJo.setBsnSrlNo(bsnSrlNo);
        fundPayPoJo.setStatus(status);
        fundPayPoJo.setStatusMessage(statusMessage);
        fundPayPoJo.setIsBig(isBig);
        fundPayPoJo.setCreateTime(createTime);
        //System.out.println(fundPayPoJo);
        fundPayMapper.addFund(fundPayPoJo);
    }

    /**
     * 日志落库
     */
    private void logFellLibrary(String interFaceNo, Object recDate, Object retTime, Object status, Object esbMsg, Object esbId, Object esbStatus, Object esbServiceCode, Object esbOut, Object esbIn, Object ybOut, Object ybIn) {
        Zjywpt_yg_trand_infos infos = new Zjywpt_yg_trand_infos();
        infos.setINTERFACENO(interFaceNo);
        infos.setRECDATE((String) recDate);
        infos.setRETDATE((String) retTime);
        infos.setSTATUS((String) status);
        infos.setESBMSG((String) esbMsg);
        infos.setESBID((String) esbId);
        infos.setESBSTATUS((String) esbStatus);
        infos.setESBServiceCode((String) esbServiceCode);
        infos.setESBOUT((String) esbOut);
        infos.setESBIN((String) esbIn);
        infos.setYBOUT((String) ybOut);
        infos.setYBIN((String) ybIn);
        //System.out.println(infos);
        trandMapper.addTrand(infos);
    }
}
